package com.liu.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Scanner;
import java.util.UUID;

import org.json.JSONObject;

import com.liu.crypto.Aes;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Message;
import android.util.Log;

public abstract class MyBlueTooth implements Runnable {

	private static String theDeviceId = null;
	private static BluetoothSocket socket = null;
	public static BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
	MyHandler mHandler;
	String msg;
	String deviceId;
	String errMsg;
	boolean isReadRes;
	String resStr;

	public abstract void OnSuccessCallBack(String resStr);

	public abstract void OnErrorCallBack(String msg);

	public void sendMsg(String msg, String deviceId, boolean isReadRes) {
		mHandler = new MyHandler() {

			@Override
			public void OnSuccess() {
				OnSuccessCallBack(resStr);
			}

			@Override
			public void OnFinish() {

			}

			@Override
			public void OnError() {
				OnErrorCallBack(errMsg);
			}
		};
		this.msg = msg;
		this.isReadRes = isReadRes;
		this.deviceId = deviceId;
		new Thread(this).start();
	}

	public static boolean checkConn(String deviceId, String txmy) {
		boolean res = false;
		try {
			BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceId);
			BluetoothSocket so = device
					.createRfcommSocketToServiceRecord(UUID.fromString("29913A2D-EB93-40cf-BBB8-DEEE26452197"));
			so.connect();
			
			JSONObject obj = new JSONObject();

			OutputStream os = so.getOutputStream();
			obj.put("type", "4");
			obj.put("v1", "");
			obj.put("v2", "");
			String tmpNr = obj.toString();
			tmpNr = Aes.Encrypt(txmy, tmpNr);

			tmpNr += "\r\n";
			os.write(tmpNr.getBytes());
			InputStream ins = so.getInputStream();
			char c = (char) ins.read();
			String resStr = "";
			while (c != -1 && c != '\0') {
				resStr += c;
				c = (char) ins.read();
			}
			ins.close();
			os.close();
			if ("1".equals(resStr)) {
				res = true;
			}
			so.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// Log.e("123", "bterr", e);
		}
		return res;
	}

	private boolean send() {
		try {
			if (socket == null || !deviceId.equals(theDeviceId)) {
				if (!deviceId.equals(theDeviceId) && socket != null) {
					try {
						socket.close();
					} catch (Exception e2) {
					}
				}
				theDeviceId = deviceId;
				BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(theDeviceId);
				socket = device
						.createRfcommSocketToServiceRecord(UUID.fromString("29913A2D-EB93-40cf-BBB8-DEEE26452197"));
				socket.connect();
			}
			OutputStream os = socket.getOutputStream();
			msg += "\r\n";
			os.write(msg.getBytes());
			InputStream ins = socket.getInputStream();

			char c = (char) ins.read();
			resStr = "";
			while (c != -1 && c != '\0') {
				resStr += c;
				c = (char) ins.read();
			}

			ins.close();
			os.close();
			socket.close();
			return true;
		} catch (Exception ex) {
			socket = null;
			errMsg = ex.getMessage();
		}
		return false;
	}

	public void run() {
		Message msg = null;
		if (!send()) {
			if (!send()) {
				msg = mHandler.obtainMessage(MyHandler.MessType_Error);
			}
		}
		if (msg == null) {
			msg = mHandler.obtainMessage(MyHandler.MessType_Succe);
		}
		mHandler.sendMessage(msg);
	}

}
